using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class TypeProductsDAO
	{
		public const string TYPEPRODUCTS_GET_BY_ID = "TypeProductsGetByID";
		public const string TYPEPRODUCTS_GET_ALL = "TypeProductsGetAll";
		public const string TYPEPRODUCTS_INSERT = "TypeProductsInsert";
		public const string TYPEPRODUCTS_UPDATE_BY_ID = "TypeProductsUpdateByID";
		public const string TYPEPRODUCTS_DELETE_BY_ID = "TypeProductsDeleteByID";

		public const string TYPEPRODUCTSID = "TypeProductsID";
		public const string NAMEFULL = "NameFull";
		public const string NAMESORT = "NameSort";
		public const string POSTION = "Postion";
		public const string CREATEDATE = "Createdate";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public TypeProductsDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public TypeProductsDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(TYPEPRODUCTS_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, TYPEPRODUCTSID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetTypeProductsFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<TypeProductsDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(TYPEPRODUCTS_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<TypeProductsDTO>();
				while (dataReader.Read())
				{
					var item = GetTypeProductsFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(TypeProductsDTO typeproducts)
		{
			var command = Database.GetStoredProcCommand(TYPEPRODUCTS_INSERT);
			try
			{
				Database.AddOutParameter(command, TYPEPRODUCTSID, DbType.String, 36);
				Database.AddInParameter(command, NAMEFULL, DbType.String, typeproducts.NameFull);
				Database.AddInParameter(command, NAMESORT, DbType.String, typeproducts.NameSort);
				Database.AddInParameter(command, POSTION, DbType.String, typeproducts.Postion);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, typeproducts.Createdate);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, TYPEPRODUCTSID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(TypeProductsDTO typeproducts)
		{
			var command = Database.GetStoredProcCommand(TYPEPRODUCTS_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, TYPEPRODUCTSID, DbType.String, typeproducts.TypeProductsID);
				Database.AddInParameter(command, NAMEFULL, DbType.String, typeproducts.NameFull);
				Database.AddInParameter(command, NAMESORT, DbType.String, typeproducts.NameSort);
				Database.AddInParameter(command, POSTION, DbType.String, typeproducts.Postion);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, typeproducts.Createdate);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string typeproductsid)
		{
			var command = Database.GetStoredProcCommand(TYPEPRODUCTS_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, TYPEPRODUCTSID, DbType.String, typeproductsid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static TypeProductsDTO GetTypeProductsFromDataReader(IDataRecord dataReader)
		{
			var item = new TypeProductsDTO();
			try
			{

				item.TypeProductsID = (string.IsNullOrEmpty(Convert.ToString(dataReader[TYPEPRODUCTSID]))) ? "" : Convert.ToString(dataReader[TYPEPRODUCTSID]);
				item.NameFull = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAMEFULL]))) ? "" : Convert.ToString(dataReader[NAMEFULL]);
				item.NameSort = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAMESORT]))) ? "" : Convert.ToString(dataReader[NAMESORT]);
				item.Postion = (string.IsNullOrEmpty(Convert.ToString(dataReader[POSTION]))) ? 0 : Convert.ToInt32(dataReader[POSTION].ToString());
				item.Createdate = (string.IsNullOrEmpty(Convert.ToString(dataReader[CREATEDATE]))) ? DateTime.MinValue : DateTime.Parse(dataReader[CREATEDATE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}

	    
	}
}

